package algorithm;

/**
 * @author 范帅帅
 */
public class ByteBusFss {

    public static int getStart(int[] gas, int[] cost) {
        int len = gas.length;
        int[] diff = new int[len * 2];
        for (int i = 0; i < len; i++) {
            diff[i] = gas[i] - cost[i];
            diff[i + len] = diff[i];
        }
        int left = 0;
        int start = 0;
        for (int i = 0; i < diff.length; i++) {
            left += diff[i];
            if (left < 0) {
                left = 0;
                start = i + 1;
            }
            if (i - start == len - 1) {
                break;
            }
        }
        if (start >= 0 && start < len) {
            return start;
        } else {
            return -1;
        }
    }

}
